package cz.cvut.fel.nur.rcp;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TextView;
import cz.cvut.fel.nur.rcp.classes.IngredientWrapper;

public class Spiz extends Activity implements OnClickListener {

	GlobalClass appState;

	Button addItem, menuUp, menuDown;

	ImageView pantry, recipes, list, options, home, scan;

	private ArrayList<TextView> ingredientViews;
	private ArrayList<ImageView> ingredientListAddViews;
	private ArrayList<TextView> ingredientQuantityViews;
	private HashMap<Integer, IngredientWrapper> buttonToIngredientMap;
	
	private TableLayout ingredientL;
	private TableLayout ingredientQuantityL;
	private TableLayout ingredientListAddL;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.spiz);
		super.onCreate(savedInstanceState);

		appState = ((GlobalClass) getApplicationContext());

		ingredientL = (TableLayout) findViewById(R.id.ingredientLayout);
		ingredientQuantityL = (TableLayout) findViewById(R.id.ingredientQuantityLayout);
		ingredientListAddL = (TableLayout) findViewById(R.id.ingredientListAddLayout);
		
//		ingredientViews = new ArrayList<TextView>();
//		ingredientQuantityViews = new ArrayList<TextView>();
//		ingredientListAddViews = new ArrayList<ImageView>();
//		buttonToIngredientMap = new HashMap<Integer, Ingredient>();
//
//		for (IngredientWrapper wrap : appState.getIngredientsInPantry()) {
//			TextView newI = new TextView(this);
//			newI.setText(wrap.ingredient.getName());
//			newI.setTextSize(20);
//			newI.setGravity(0);
//			newI.setPadding(10, 10, 0, 10);
//			newI.setTextColor(-16777216);
//			newI.setId(wrap.ingredient.hashCode());
//			newI.setOnClickListener((android.view.View.OnClickListener) this);
//			newI.setClickable(true);
//			ingredientL.addView(newI, 0);
//			buttonToIngredientMap.put(newI.getId(), wrap.ingredient);
//			
//			TextView newIQ = new TextView(this);
//			newIQ.setText(wrap.quantity + wrap.ingredient.getQuantityType().meaning);
//			newIQ.setTextSize(20);
//			newIQ.setGravity(0);
//			newIQ.setPadding(10, 10, 0, 10);
//			newIQ.setTextColor(-16777216);
//			newIQ.setId((wrap.ingredient.getName() + wrap.ingredient.getQuantityType().meaning + "Quantity").hashCode());
//			newIQ.setOnClickListener((android.view.View.OnClickListener) this);
//			newIQ.setClickable(true);
////			newIQ.setBackgroundColor(-2236962);
//			ingredientQuantityL.addView(newIQ, 0);
//			buttonToIngredientMap.put(newIQ.getId(), wrap.ingredient);
//			
//			ImageView newILA = new ImageView(this);
//			newILA.setAdjustViewBounds(true);
//			newILA.setId((wrap.ingredient.getName() + wrap.ingredient.getQuantityType().meaning + "ListAdd").hashCode());			
//			newILA.setImageResource(R.drawable.file);
//			newILA.setMaxHeight(65);
//			newILA.setMaxWidth(65);
//			newILA.setOnClickListener((android.view.View.OnClickListener) this);
//			newILA.setClickable(true);
//			ingredientListAddL.addView(newILA, 0);
//			buttonToIngredientMap.put(newILA.getId(), wrap.ingredient);
//			
//			ingredientViews.add(newI);
//			ingredientQuantityViews.add(newIQ);
//			ingredientListAddViews.add(newILA);
//		}
		
		
		
		addItem = (Button) findViewById(R.id.addItemButton);
		addItem.setOnClickListener((android.view.View.OnClickListener) this);
		menuDown = (Button) findViewById(R.id.menuDownButton);
		menuDown.setOnClickListener((android.view.View.OnClickListener) this);
		menuUp = (Button) findViewById(R.id.menuUpButton);
		menuUp.setOnClickListener((android.view.View.OnClickListener) this);
		
		pantry = (ImageView) findViewById(R.id.pantryButton);
		pantry.setOnClickListener((android.view.View.OnClickListener) this);
		recipes = (ImageView) findViewById(R.id.recipesButton);
		recipes.setOnClickListener((android.view.View.OnClickListener) this);
		list = (ImageView) findViewById(R.id.listButton);
		list.setOnClickListener((android.view.View.OnClickListener) this);
		options = (ImageView) findViewById(R.id.optionsButton);
		options.setOnClickListener((android.view.View.OnClickListener) this);
		home = (ImageView) findViewById(R.id.homeButton);
		home.setOnClickListener((android.view.View.OnClickListener) this);
		scan = (ImageView) findViewById(R.id.scanButton);
		scan.setOnClickListener((android.view.View.OnClickListener) this);

		// visibility
		menuDown.setVisibility(View.GONE); // 0 is visible
		pantry.setVisibility(View.GONE);
		recipes.setVisibility(View.GONE);
		list.setVisibility(View.GONE);
		options.setVisibility(View.GONE);
		home.setVisibility(View.GONE);

	}
	
	public void onResume() {
		super.onResume();
		
		ingredientL.removeAllViews();
		ingredientQuantityL.removeAllViews();
		ingredientListAddL.removeAllViews();
		
		ingredientViews = new ArrayList<TextView>();
		ingredientQuantityViews = new ArrayList<TextView>();
		ingredientListAddViews = new ArrayList<ImageView>();
		buttonToIngredientMap = new HashMap<Integer, IngredientWrapper>();

		for (IngredientWrapper wrap : appState.getIngredientsInPantry()) {
			TextView newI = new TextView(this);
			newI.setText(wrap.ingredient.getName());
			newI.setTextSize(20);
			newI.setGravity(0);
			newI.setPadding(10, 10, 0, 10);
			newI.setTextColor(-16777216);
			newI.setId(wrap.ingredient.hashCode());
			newI.setOnClickListener((android.view.View.OnClickListener) this);
			newI.setClickable(true);
			ingredientL.addView(newI, 0);
			buttonToIngredientMap.put(newI.getId(), wrap);
			
			TextView newIQ = new TextView(this);
			newIQ.setText(wrap.quantity + wrap.ingredient.getQuantityType().meaning);
			newIQ.setTextSize(20);
			newIQ.setGravity(0);
			newIQ.setPadding(10, 10, 0, 10);
			newIQ.setTextColor(-16777216);
			newIQ.setId((wrap.ingredient.getName() + wrap.ingredient.getQuantityType().meaning + "Quantity").hashCode());
			newIQ.setOnClickListener((android.view.View.OnClickListener) this);
			newIQ.setClickable(true);
//			newIQ.setBackgroundColor(-2236962);
			ingredientQuantityL.addView(newIQ, 0);
			buttonToIngredientMap.put(newIQ.getId(), wrap);
			
			ImageView newILA = new ImageView(this);
			newILA.setAdjustViewBounds(true);
			newILA.setId((wrap.ingredient.getName() + wrap.ingredient.getQuantityType().meaning + "ListAdd").hashCode());			
			newILA.setImageResource(R.drawable.file);
			newILA.setMaxHeight(61);
			newILA.setMaxWidth(61);
			newILA.setOnClickListener((android.view.View.OnClickListener) this);
			newILA.setClickable(true);
			ingredientListAddL.addView(newILA, 0);
			buttonToIngredientMap.put(newILA.getId(), wrap);
			
			ingredientViews.add(newI);
			ingredientQuantityViews.add(newIQ);
			ingredientListAddViews.add(newILA);
		}
		
	}
	
	

	public void onClick(View v) {
		if (v.getId() == R.id.menuDownButton) {
			menuDown.setVisibility(View.GONE);
			pantry.setVisibility(View.GONE);
			recipes.setVisibility(View.GONE);
			list.setVisibility(View.GONE);
			options.setVisibility(View.GONE);
			home.setVisibility(View.GONE);
		}	
		if (v.getId() == R.id.menuUpButton) {
			menuDown.setVisibility(0); 
			pantry.setVisibility(0);
			recipes.setVisibility(0);
			list.setVisibility(0);
			options.setVisibility(0);
			home.setVisibility(0);
		}	
		
		for (TextView tv : ingredientViews) {
			if (tv.getId() == v.getId()) {
				appState.setModifiedIngredient(buttonToIngredientMap.get(tv.getId()));
				startActivity(new Intent(this, SpizDetail.class));
				break;
			}
		}
		
		for (TextView tv : ingredientQuantityViews) {
			if (tv.getId() == v.getId()) {
				appState.setModifiedIngredient(buttonToIngredientMap.get(tv.getId()));
				startActivity(new Intent(this, SpizDetail.class));
				break;
			}
		}
		
		for (ImageView iv : ingredientListAddViews) {
			if (iv.getId() == v.getId()) {
				appState.setModifiedIngredient(buttonToIngredientMap.get(iv.getId()));
				startActivity(new Intent(this, SpizSeznamPridat.class));
				break;
			}
		}
		
		
		if (v.getId() == R.id.addItemButton) {
			startActivity(new Intent(this, SpizPridat.class));
		}
		
		/* obrazky */
		if (v.getId() == R.id.pantryButton) {
			startActivity(new Intent(this, Spiz.class));
		}
		if (v.getId() == R.id.recipesButton) {
			startActivity(new Intent(this, Recepty.class));
		}
		if (v.getId() == R.id.listButton) {
			startActivity(new Intent(this, NakupniSeznam.class));
		}
		if (v.getId() == R.id.optionsButton) {
			startActivity(new Intent(this, Nastaveni.class));
		}
		if (v.getId() == R.id.homeButton) {
			startActivity(new Intent(this, Main.class));
		}
		if (v.getId() == R.id.scanButton) {
			startActivity(new Intent(this, Scan.class));
		}
	}

}
